package com.guodu.service.impl;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.guodu.dao.MaterialStoreDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.MaterialStore;
import com.guodu.entity.MaterialstoreStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.MaterialStoreService;
import com.guodu.vo.MaterialStoreQueryVo;
import com.guodu.vo.MaterialStoreVo;

@Service("materialStoreService")
public class MaterialStoreServiceImpl implements MaterialStoreService {

	@Resource
	private MaterialStoreDao materialStoreDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	
	@Override
	public List<MaterialStoreVo> findByVo(MaterialStoreQueryVo materialStoreQueryVo,Units units,
			int curPage, int pageSize) {
		// TODO Auto-generated method stub
		List<MaterialStore> list = materialStoreDao.findByVo(materialStoreQueryVo,units, curPage, pageSize);
		List<MaterialStoreVo> materialStoreVos = new ArrayList<MaterialStoreVo>();
		for(MaterialStore m :list){
			MaterialStoreVo materialStoreVo = new MaterialStoreVo(m);
			UserInfo userInfo  = userInfoDao.findById(m.getCreatePeople());
			materialStoreVo.setUserName(userInfo.getUserName());
			if(userInfo.getUnitsId()!=null){
			Units unitsInfo = unitsDao.findById(userInfo.getUnitsId());
			if(unitsInfo!=null){
				materialStoreVo.setUnitsLevel(unitsInfo.getUnitsLevel());
			}
			}
			Units units1 = unitsDao.findById(Long.valueOf(m.getUnitsId()));
			Units funits1 = unitsDao.findById(units1.getFunitsId());
			materialStoreVo.setFunitsName(funits1.getUnitsName());
			materialStoreVo.setIndustry(units1.getIndustry());
			materialStoreVo.setUnitsName(units1.getUnitsName());
			materialStoreVos.add(materialStoreVo);
		}
		return materialStoreVos;
	}

	@Override
	public int findSizeByVo(MaterialStoreQueryVo materialStoreQueryVo,Units units) {
		// TODO Auto-generated method stub
		return materialStoreDao.findSizeByVo(materialStoreQueryVo,units);
	}

	@Override
	public MaterialStore findById(String id) {
		// TODO Auto-generated method stub
		return materialStoreDao.findById(id);
	}

	@Override
	public void del(String id) {
		// TODO Auto-generated method stub
		materialStoreDao.del(id);
	}

	@Override
	public void delByIds(String[] ids) {
		// TODO Auto-generated method stub
		materialStoreDao.delByIds(ids);
	}

	@Override
	public void save(MaterialStore entity) {
		// TODO Auto-generated method stub
		materialStoreDao.save(entity);
	}

	@Override
	public void examine(String ids,String reason,String beforeStatus,Long afterStatus,UserInfo userInfo) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<MaterialstoreStatus> entities = new ArrayList<MaterialstoreStatus>();
		for(int i=0;i<idses.length;i++){
			MaterialstoreStatus entity = new MaterialstoreStatus();
			entity.setAfterStatus(afterStatus);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(userInfo.getUserId());
			entity.setMaterialId(idses[i]);
			entity.setReason(reason);
			entity.setStatusId(i+"");
			entity.setCreateTime(Calendar.getInstance().getTime());
			entities.add(entity);
		}
		materialStoreDao.changeStatus(entities);
		materialStoreDao.examine(ids,afterStatus);
	}

	@Override
	public void update(MaterialStore entity) {
		// TODO Auto-generated method stub
		materialStoreDao.update(entity);
	}

	@Override
	public List<MaterialstoreStatus> findStatus(String id) {
		// TODO Auto-generated method stub
		return materialStoreDao.findStatus(id);
	}

	@Override
	public void delByStoreId(String storeId) {
		// TODO Auto-generated method stub
		materialStoreDao.delByStoreId(storeId);
	}
}
